#import "MapAppAppDelegate.h"
#import "MapViewController.h"

/**
 * Initializes and displays the main view controller for this application.
 * 
 * @author      Justin Hollander
 * @version     0.2.0 
 * @since       0.1.0
 */
@implementation MapAppAppDelegate

@synthesize window;
@synthesize mapViewController;
@synthesize navigationController;

/**
 * Initializes basic UIWindow with main components then sets to visible.
 *
 * This is all done programically without the use of nibs.
 *
 * @param application  the application that has just launched
 */
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    isDrawPin = NO;
	
	//window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	//window.backgroundColor = [UIColor redColor];
	mapViewController = [[MapViewController alloc] init];
	touchView = [[MapTouchTestView alloc] initWithMapView:mapViewController];
	window = [[TouchableWindow alloc] initWithView:mapViewController.view delegate:touchView];
	
	//UIViewController* viewController = [[UIViewController alloc] init];
	//[viewController.view addSubview:touchView];
	//viewController.navigationController.navigationBar.hidden = YES;
	navigationController = [[UINavigationController alloc] initWithRootViewController:mapViewController];
	
	//navigationController = [[UINavigationController alloc] initWithRootViewController:mapViewController];
	
	[window addSubview:navigationController.view];
	//[navigationController seth
	//[window addSubview:mapViewController.view];
    [window makeKeyAndVisible];
	
	
	[self updateNavigationBar];
}

/// TEST METHOD
- (void)updateNavigationBar {
	UIBarButtonItem* rightButton;
	
	if (!isDrawPin) {
		rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Turn On" 
													   style:UIBarButtonItemStyleDone 
													  target:self 
													  action:@selector(toggleDrawPin)];
	} else {
		rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Turn Off" 
													   style:UIBarButtonItemStyleDone 
													  target:self 
													  action:@selector(toggleDrawPin)];
	}
	[mapViewController.navigationItem setRightBarButtonItem:rightButton];
	[rightButton release];
}

- (void)toggleDrawPin {
	isDrawPin = !isDrawPin;
	touchView.isActive = isDrawPin;
	[self updateNavigationBar];
}

- (void)dealloc {
	[mapViewController release];
	[navigationController release];
	
    [window release];
    [super dealloc];
}

@end